// Purpose: Debug and unit test functions
//
// Created: Daly 2012-04-01

#ifndef _AX_DEBUG_H_
#define _AX_DEBUG_H_


//unit test declaration======================================

//implement it in your module
#define IMPLEMENT_UTEST(name) void _utest_##name() 
//run it!
#define RUN_UTEST(name) {extern void _utest_##name(); _utest_##name(); }


#define AX_ASSERT(expr) (void)((expr) || (ax_assert(#expr, __FILE__, __FUNCTION__, __LINE__), 0))
#define UTEST_ASSERT(expr) (void)((expr) || (ax_debug("UTEST FAIL: %s %s->%s %d", #expr, __FILE__, __FUNCTION__, __LINE__), 0))


//output debug message in a log file instead of debug.log
//Usage(in your module's cpp file):
//	DECLARE_DEBUG_FUNC(mymodule)
//	#define debug _debug_mymodule
//	
//	debug("my message %d", 1234);
//

//open by default
#if 0 
	#define DECLARE_DEBUG_FUNC(sec) \
	void _debug_##sec(const char* fmt, ...) { }
#else
#define DECLARE_DEBUG_FUNC( sec )   \
	static void _debug_##sec(const char* fmt, ...) \
	{ 							\
		static char file[256];  \
		va_list args;           \
		va_start(args, fmt);    \
		sprintf(file, "%s_debug.log", #sec); \
		ax_vlogf(file, fmt, args); \
		va_end(args); \
	}
#endif

void ax_assert(const char *expr, const char* file, const char* func, int line);
void ax_vlogf(const char* file, const char* fmt, va_list arg);
void ax_debug(const char *fmt, ...);
void ax_utest_log(const char *fmt, ...);
void ax_logfile(const char* file, const char* fmt, ...);
void set_log_path(const char* path);
void ax_format_time(char *buf, int n);
void switch_debug_log(int enable);

#endif

